﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tizer.Core.Repository;
using Tizer.Web.Backoffice.Controls;
using Tizer.Web.Helper;

namespace Tizer.Web.Backoffice
{
    /// <summary>
    /// Редактирование города
    /// </summary>
    public partial class CityEdit : AdminBasePage
    {
        /// <summary>
        /// Инициализация страницы
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Init(object sender, EventArgs e)
        {
            BindCountryList();
        }

        /// <summary>
        /// Загрузка страницы
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.SetPageTitle();
            if (!Page.IsPostBack)
            {
                var id = QueryHelper.Value<int>("id", 0);
                if (id == 0)
                    Response.Redirect(UrlAdminDashboard);
                else
                {
                    using (var cityRepository = new CityRepository())
                    {
                        var city = cityRepository.GetById(id);
                        if (city != null)
                        {
                            txtName.Text = city.Name;
                            if (city.CountryID != null) ddlCountry.SelectedIndex = (int)city.CountryID;
                            else ddlCountry.SelectedIndex = 0;
                        }
                        else
                            Response.Redirect(UrlAdminDashboard);
                    }
                }
            }
        }

        /// <summary>
        /// Сохранение данных
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Edit(object sender, EventArgs e)
        {
            bool flagMessageShow = false;

            var id = QueryHelper.Value<int>("id", 0);
            if (id == 0)
            {
                Response.Redirect(UrlAdminDashboard);
            }
            else
            {
                if (ddlCountry.SelectedItem.Value != "0")
                {
                    using (var cityRepository = new CityRepository())
                    {
                        var city = cityRepository.GetById(id);
                        if (city != null)
                        {
                            if (!string.IsNullOrEmpty(txtName.Text))
                            {
                                city.Name = txtName.Text;
                                city.CountryID = Convert.ToInt32(ddlCountry.SelectedItem.Value);
                                cityRepository.SaveChanges();
                                Response.Redirect(UrlAdminCityList);
                            }
                        }
                    }
                }
                else
                {
                    tizerMessage.SetMessage("Внимание", "Страна не выбрана!", MessageType.Warning);
                    flagMessageShow = true;
                }
            }

            if (!flagMessageShow)
                tizerMessage.SetMessage("Ошибка", "Невозможно обновить город", MessageType.Error);
        }

        /// <summary>
        /// Заполнение списка стран
        /// </summary>
        private void BindCountryList()
        {
            var emptyItem = new ListItem { Text = StrNotChoosed, Value = "0" };
            ddlCountry.Items.Add(emptyItem);

            using (var countryRepository = new CountryRepository())
            {
                var countries = countryRepository.GetAll();
                if (countries != null)
                {
                    foreach (var country in countries)
                    {
                        var item = new ListItem { Text = country.Name, Value = country.ID.ToString() };
                        ddlCountry.Items.Add(item);
                    }
                }
            }
        }
    }
}